package com.springboot.cloud.demos.producer.rest;

import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.web.redis.CacheDuration;
import com.springboot.cloud.common.web.redis.ExpireTimeConstant;
import com.springboot.cloud.demos.entity.form.UserPositionForm;
import com.springboot.cloud.demos.entity.param.UserPositionQueryParam;
import com.springboot.cloud.demos.entity.po.UserPosition;
import com.springboot.cloud.demos.producer.service.IUserPositionService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author qjmove
 */
@RestController
@RequestMapping("/userPosition")
@Api("userPosition")
@Slf4j
@CacheDuration(duration = ExpireTimeConstant.ONE_DAY)
public class UserPositionController {

    @Autowired
    private IUserPositionService userPositionService;

    @ApiOperation(value = "新增产品", notes = "新增一个产品")
    @ApiImplicitParam(name = "userPositionForm", value = "新增产品form表单", required = true, dataType = "UserPositionForm")
    @PostMapping
    public Result add(@Valid @RequestBody UserPositionForm userPositionForm) {
        log.info("name:{}", userPositionForm);
        UserPosition userPosition = userPositionForm.toPo(UserPosition.class);
        return Result.success(userPositionService.add(userPosition));
    }

    @ApiOperation(value = "删除产品", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "产品ID", required = true, dataType = "long")
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable String id) {
        return Result.success(userPositionService.delete(id));
    }

    @ApiOperation(value = "修改产品", notes = "修改指定产品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "产品ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "userPositionForm", value = "产品实体", required = true, dataType = "UserPositionForm")
    })
    @PutMapping(value = "/{id}")
    public Result update(@PathVariable String id, @Valid @RequestBody UserPositionForm userPositionForm) {
        UserPosition userPosition = userPositionForm.toPo(UserPosition.class);
        userPosition.setId(id);
        return Result.success(userPositionService.update(userPosition));
    }

    @ApiOperation(value = "获取产品", notes = "获取指定产品信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "产品ID", required = true, dataType = "long")
    @GetMapping(value = "/{id}")
    public Result<UserPosition> get(@PathVariable String id) {
        log.info("get with id:{}", id);
        return Result.success(userPositionService.get(id));
    }

    @ApiOperation(value = "查询产品", notes = "根据条件查询产品信息，简单查询")
    @ApiImplicitParam(paramType = "query", name = "userId", value = "用户Id", required = true, dataType = "string")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @GetMapping("/query")
    public Result<UserPosition> query(@RequestParam String userId) {
        log.info("query with name:{}", userId);
        UserPositionQueryParam param = new UserPositionQueryParam(userId);
        List list = userPositionService.query(param);
        if (list == null || list.isEmpty()) {
            UserPositionForm userPositionForm= new UserPositionForm();
            userPositionForm.setUserId(userId);
            Result.success( this.add(userPositionForm));
        }
        return Result.success(list.get(0));
    }
}

